<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button class="get">get</button>
    <button class="post">post</button>
</body>
<script>
    var getBtn = document.getElementsByClassName("get")[0];
    var postBtn = document.getElementsByClassName("post")[0];

    getBtn.onclick = function () {
        var wd = ""; //全局变量  记录搜索的关键词   "" => 默认查询所有数据
        var col = "id"; // 全局变量  按哪个数据排序  默认按id排序
        var type = "asc"; //  全局变量  排序方式   默认升序
        var page = 1;   // 全局变量  记录页码(默认显示第一页)
        var size = 5;   // 全局变量  记录每页显示几条数据


        var xhr = new XMLHttpRequest();

        // ajax get传参 => 将参数数据队列拼接到请求地址之后,用?分隔

        // xhr.open("get", "http://43.138.81.225/demo/php/searchGoodsOrderLimit.php?wd=" + wd + "&col=" + col + "&type=" + type + "&page=" + page + "&size=" + size, true);
        xhr.open("get", `http://43.138.81.225/demo/php/searchGoodsOrderLimit.php?wd=${wd}&col=${col}&type=${type}&page=${page}&size=${size}`, true);

        xhr.send();

        xhr.onreadystatechange = function () {
            // 2 3 4
            if (xhr.readyState == 4 && xhr.status == 200) {
                var result = xhr.responseText;
                // console.log("请求成功", result);

                result = JSON.parse(result);
                console.log(result);

                // code后端接口返回的信息  -> 请求成功 失败 
                // var { status, message, list } = result;

                // if (status) {
                //     var html = "";

                //     list.forEach(({ goodsName, goodsImg, goodsPrice }) => {
                //         html += `<li>
                //         <a href="">
                //             <img src="${goodsImg}"
                //                 alt="">
                //             <div class="price">${goodsPrice}</div>
                //             <div class="name">${goodsName}</div>
                //         </a>
                //     </li>`;
                //     });

                //     showGoods.innerHTML = html;
                // }

            }
        }
    }


    postBtn.onclick = function () {
        var acccount = "a123123";
        var pwd = "123123";

        var xhr = new XMLHttpRequest();

        xhr.open("post", "http://43.138.81.225/demo/php/login_account.php", true);

        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send(`account=${acccount}&pwd=${pwd}`);

        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                var result = xhr.responseText;
                result = JSON.parse(result);

                console.log(result);

                // var { status, message } = result;

                // if (status) {
                //     location.href = "../index.html";
                // } else {
                //     alert(message);
                // }

            }
        }
    }

</script>




</html>